package com.topnav.mc.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.js.lib.result.ApiResult;
import com.js.lib.result.Result;
import com.topnav.mc.admin.entity.DevicePosition;
import com.topnav.mc.admin.entity.bean.DevicePositionBean;
import com.topnav.mc.admin.entity.param.DevicePositionParam;
import com.topnav.mc.admin.entity.wrapper.DevicePositionWrapper;
import com.topnav.mc.admin.service.IDevicePositionService;
import com.topnav.mc.base.mc.McController;
import com.topnav.mc.base.mc.McPage;
import com.topnav.mc.exception.ExceptionMsg;
import com.topnav.mc.position.PositionUtil;
import com.topnav.mc.uitls.ResultErrorUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "admin/device/pos", tags = "设备定位信息")
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("admin/device/pos")
public class DevicePositionController extends McController<DevicePosition, DevicePositionBean, DevicePositionParam,
        IDevicePositionService,
        DevicePositionWrapper> {

    @Autowired
    private IDevicePositionService devicePositionService;

    @Autowired
    PositionUtil positionUtil;

    @PostMapping("/get/current/{deviceCode}")
    @ResponseBody
    public Result getCurrentPosition(@PathVariable String deviceCode) {
        return Result.ok(positionUtil.getCurrentPosition(deviceCode));
    }

    @PostMapping("/query/page")
    @Override
    public ApiResult<List<DevicePositionBean>> queryPage(@Validated(IPage.class) @RequestBody McPage<DevicePositionParam> page) {
        if (page.getWhere() ==null){
            log.error("条件参数时间和设备必须提供！");
            return (ApiResult<List<DevicePositionBean>>) ResultErrorUtil.apibyExceptionMsg(ExceptionMsg.ERROR.ERROR_PARAM_TIME_AND_DEVICE_NOT_PROVIDED);
        }

        if (page.getWhere().getBeginTime() ==null || page.getWhere().getEndTime() ==null  ){
            log.error("参数 时间 必须提供");
            return (ApiResult<List<DevicePositionBean>>) ResultErrorUtil.apibyExceptionMsg(ExceptionMsg.ERROR.ERROR_PARAM_TIME_NOT_PROVIDED);
        }

        if (page.getWhere().getDeviceId() ==null){
            log.error("参数设备必须提供");
            return (ApiResult<List<DevicePositionBean>>) ResultErrorUtil.apibyExceptionMsg(ExceptionMsg.ERROR.ERROR_PARAM_DEVICE_NOT_PROVIDED);
        }

        return super.queryPage(page);
    }

    /**
     * 查询定位信息明细
     * @param page
     * @return
     */
    @PostMapping("/query/page/for/detail")
    public ApiResult<List<DevicePositionBean>> queryPageForDetail(@Validated(IPage.class) @RequestBody McPage<DevicePositionParam> page) {
        return super.queryPage(page);
    }

    /**
     * 轨迹回放查询
     * @param page
     * @return
     */
    @PostMapping("/query/page/by/track")
    @ResponseBody
    public ApiResult<List<DevicePositionBean>> queryForTrackPlay(@Validated(IPage.class) @RequestBody McPage<DevicePositionParam> page) {
        if (page.getWhere() ==null){
            log.error("条件参数时间和设备必须提供！");
            return (ApiResult<List<DevicePositionBean>>) ResultErrorUtil.apibyExceptionMsg(ExceptionMsg.ERROR.ERROR_PARAM_TIME_AND_DEVICE_NOT_PROVIDED);

        }

        if (page.getWhere().getBeginTime() ==null || page.getWhere().getEndTime() ==null  ){
            log.error("参数 时间 必须提供");
            return (ApiResult<List<DevicePositionBean>>) ResultErrorUtil.apibyExceptionMsg(ExceptionMsg.ERROR.ERROR_PARAM_TIME_NOT_PROVIDED);
        }

        if (page.getWhere().getDeviceId() ==null){
            log.error("参数设备必须提供");
            return (ApiResult<List<DevicePositionBean>>) ResultErrorUtil.apibyExceptionMsg(ExceptionMsg.ERROR.ERROR_PARAM_DEVICE_NOT_PROVIDED);
        }

        Page<DevicePositionBean> page1 = devicePositionService.pageInfo(page.getPage(), getNewWrapper(page.getWhere()));

        return (ApiResult<List<DevicePositionBean>>) ApiResult.ok(page1);

    }

}
